'''
An RPG Modification for Source games using Source Python.

    Copyright (C) 2013 Steven Hartin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Created on 28 Jan 2013

@author: Steven Hartin
@file: ../sourcerpg/popups/downgrade_popup.py
'''

__all__ = ["DowngradePopup"]

from players.helpers import userid_from_index

from sourcerpg.popuplib import EasyMenu
from sourcerpg.skills import skills
from sourcerpg import player as player_module
from sourcerpg import skills as skills_module


class DowngradePopup(EasyMenu):
    """This class creates the sell skills menu for an individual player"""
    def __init__(self, player):
        """@param player.Player Player instance for this popup"""
        assert isinstance(player, player_module.Player)

        super(__class__, self).__init__()
        self.player = player
        self.title = "Downgrade Skills (%d credits)" % player["credits"]

    def add_skills(self):
        """Loops through all loaded skills and adds them to the popup"""
        for skill_instance in skills.values():
            self.add_skill(skill_instance)

    def add_skill(self, skill):
        """Adds a skill into the player's popup

        This makes sure that the user can actually upgrade the skill, and
        if they can't, the item is made selectable. The only reason a player
        may not be able to upgrade their skill is if they don't have enough
        credits or they have already reached their maximum level

        @param skill skills.SkillBase The skill to add to the popup

        """
        assert isinstance(skill, skills_module.SkillBase)

        level = self.player.get_skill_level(skill.internal_name)
        cost = skill.get_credit_cost(level)
        if level > 0:
            # We can upgrade this further, make sure we notify them of
            # the level difference and cost
            text = "%s (%d => %d) [%d Credits gained]" % (skill.display_name,
                level, level - 1, cost * 0.75)
        else:
            # We've reached maximum, cannot upgrade further
            text = "%s (%d) [NEED A LEVEL]" % (skill.display_name,
                level)
        self.add_option(text, self.downgrade_skill_popup_callback,
            level > 0, skill.internal_name)

    def downgrade_skill_popup_callback(self, user, choice, args):
        """Callback from option call, downgrade the skill"""
        userid = userid_from_index(user)
        player = player_module.players[userid]
        player.downgrade_skill(args[0])
        self.send(user)
